package com.brillilab.starter.controller.aio.experiments;

import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.enums.experiment.PlateTypeEnum;
import com.brillilab.domain.pa.experiments.PlateAdd;
import com.brillilab.domain.pa.experiments.PlateReagentAdd;
import com.brillilab.domain.pa.experiments.PlateTextAdd;
import com.brillilab.domain.pa.experiments.PlateTimerAdd;
import com.brillilab.domain.po.experiments.Plate;
import com.brillilab.domain.vo.experiments.*;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.logic.experiments.ExperimentPlateLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;
import com.brillilab.starter.entity.req.experiment.GridReagentDelete;
import com.brillilab.starter.entity.req.experiment.GridTextDelete;
import com.brillilab.starter.entity.req.experiment.GridTimerDelete;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;

/**
 * 实验多孔板
 * @author Wu Menghao
 * @time   2019/04/16
 */
@RestController
@RequestMapping(value="/api/experiment/plate")
public class ExperimentPlateController {

    @Resource
    private UsersLogic usersLogic;
    @Resource
    private ExperimentPlateLogic experimentPlateLogic;


    /**
     * 多孔板添加实验分组
     * @param vo
     * @return
     */
    @PostMapping("/group/add")
    public ResponseVo addPlateGridGroup(@RequestBody PlateGridGroupAddVo vo) {
        Assert.isTrue(vo != null, "请求参数缺失！");
        Assert.isTrue(vo.getExperimentId() != null&&vo.getPlateId() != null, "请求参数缺失！");
        experimentPlateLogic.addPlateGridGroup(vo);
        return ResponseVo.success();
    }

    /**
     * 多孔板删除实验分组
     * @param ids
     * @return
     */
    @PostMapping("/group/delete")
    public ResponseVo deletePlateGridGroup(@RequestBody List<Long> ids) {
        Assert.isTrue(!CollectionUtils.isEmpty(ids), "请求参数缺失！");
        experimentPlateLogic.deletePlateGridGroup(ids);
        return ResponseVo.success();
    }

    /**
     * 多孔板类型列表
     * @param token
     * @return
     */
    @RequestMapping(value="/types",method=RequestMethod.GET)
    public ResponseVo plateTypes(@RequestHeader(name=StarterConstant.TOKEN) String token){

        Assert.isTrue(StringUtils.isNotBlank(token),"参数缺失！");

        List<PlateTypeGroupVo> groupVos = experimentPlateLogic.getPlateTypeGroups();

        return ResponseVo.success(groupVos);
    }

    /**
     * 多孔板可选列表
     * @param experimentId
     * @param phaseId
     * @param token
     * @return
     */
    @RequestMapping(value="/choose/list",method=RequestMethod.GET)
    public ResponseVo plateList(Long experimentId,Long phaseId,@RequestHeader(name=StarterConstant.TOKEN) String token){

        Assert.isTrue(experimentId!=null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        List<PlateTypeGroupVo> plateList = experimentPlateLogic.getPhaseUsablePlateList(userInfo.getLabId(),experimentId,phaseId);

        return ResponseVo.success(plateList);
    }

    /**
     * 多孔板选择添加/新建
     * @param plateAdd
     * @param token
     * @return
     */
    @AspectLog
    @RequestMapping(value="/add",method=RequestMethod.POST)
    public ResponseVo plateAdd(@RequestBody PlateAdd plateAdd,@RequestHeader(name=StarterConstant.TOKEN) String token){

        Assert.isTrue(plateAdd!=null && plateAdd.getExperimentId()!=null,"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        Plate rs = experimentPlateLogic.createPlateOrBindExistPlateToStep(plateAdd,userInfo.getLabId(),userInfo.getLabMemberId());

        return ResponseVo.success(rs);
    }


    /**
     * 多孔板删除
     * @param experimentId
     * @param plateId
     * @param token
     * @return
     */
    @AspectLog
    @RequestMapping(value="/delete",method=RequestMethod.DELETE)
    public ResponseVo plateDelete(Long experimentId,Long plateId,@RequestHeader(name=StarterConstant.TOKEN) String token){

        Assert.isTrue(experimentId!=null && plateId!=null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        experimentPlateLogic.deletePlate(experimentId,plateId,userInfo.getLabId(),userInfo.getLabMemberId());

        return ResponseVo.success();
    }

    /**
     * 获取单块多孔板信息
     * @param experimentId
     * @param plateId
     * @param token
     * @return
     */
    @RequestMapping(value="/info",method=RequestMethod.GET)
    public ResponseVo plateInfo(Long experimentId,Long plateId,@RequestHeader(name=StarterConstant.TOKEN) String token){

        Assert.isTrue(plateId!=null && experimentId!=null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        PlateVo plateVo = experimentPlateLogic.getPlateInfo(experimentId,plateId,userInfo.getLabId());

        return ResponseVo.success(plateVo);
    }

    /**
     * 获取单块多孔板预览
     * @param experimentId
     * @param plateId
     * @param token
     * @return
     */
    @RequestMapping(value="/preview",method=RequestMethod.GET)
    public ResponseVo platePreview(Long experimentId,Long plateId,@RequestHeader(name=StarterConstant.TOKEN) String token){

        Assert.isTrue(plateId!=null && experimentId!=null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        PlateVo plateVo = experimentPlateLogic.getPlatePreview(experimentId,plateId,userInfo.getLabId());

        return ResponseVo.success(plateVo);
    }

    /**
     * 多孔板列表
     * @param experimentId
     * @param phaseId
     * @param stepId
     * @param token
     * @return
     */
    @RequestMapping(value="/list",method=RequestMethod.GET)
    public ResponseVo plateList(Long experimentId,Long phaseId,Long stepId,@RequestHeader(name=StarterConstant.TOKEN) String token){

        Assert.isTrue(experimentId!=null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        List<PlateVo> plateVos = experimentPlateLogic.getStepPlateList(experimentId,phaseId,stepId,userInfo.getLabId());

        return ResponseVo.success(plateVos);
    }


    /**
     * 多孔板添加计时器
     * @param plateTimerAdd
     * @param token
     * @return
     */
    @AspectLog
    @RequestMapping(value="/timer/add",method=RequestMethod.POST)
    public ResponseVo addTimerToPlateGrid(@RequestBody PlateTimerAdd plateTimerAdd,@RequestHeader(name=StarterConstant.TOKEN) String token){

        Assert.isTrue(plateTimerAdd!=null && StringUtils.isNotBlank(token) && plateTimerAdd.getExperimentId()!=null
                && plateTimerAdd.getPlateId()!=null && plateTimerAdd.getTimerId()!=null ,"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        experimentPlateLogic.addTimerToPlateGrid(plateTimerAdd,userInfo.getLabId(),userInfo.getLabMemberId());

        return ResponseVo.success();
    }

    /**
     * 多孔板添加试剂/样品
     * @param plateReagentAdd
     * @param token
     * @return
     */
    @AspectLog
    @RequestMapping(value="/reagent/add",method=RequestMethod.POST)
    public ResponseVo addMaterialToPlateGrid(@RequestBody PlateReagentAdd plateReagentAdd,@RequestHeader(name=StarterConstant.TOKEN) String token){

        Assert.isTrue(plateReagentAdd!=null && StringUtils.isNotBlank(token)
                && plateReagentAdd.getExperimentId()!=null && plateReagentAdd.getStepReagentId()!=null
                && plateReagentAdd.getType()!=null && !CollectionUtils.isEmpty(plateReagentAdd.getGridIds()),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        experimentPlateLogic.addMaterialToPlateGrid(plateReagentAdd,userInfo.getLabId(),userInfo.getLabMemberId());

        return ResponseVo.success();
    }

    /**
     * 多孔板添加文本
     * @param plateTextAdd
     * @param token
     * @return
     */
    @AspectLog
    @RequestMapping(value="/text/add",method=RequestMethod.POST)
    public ResponseVo addTextToPlateGrid(@RequestBody PlateTextAdd plateTextAdd,@RequestHeader(name=StarterConstant.TOKEN) String token){

        Assert.isTrue(plateTextAdd!=null && StringUtils.isNotBlank(token) && !CollectionUtils.isEmpty(plateTextAdd.getGridIds())
                && plateTextAdd.getExperimentId()!=null  && plateTextAdd.getStepTextId()!=null,"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        experimentPlateLogic.addTextToPlateGrid(plateTextAdd,userInfo.getLabId(),userInfo.getLabMemberId());

        return ResponseVo.success();
    }

    /**
     * 多孔板删除计时器
     * @param delete
     * @param token
     * @return
     */
    @AspectLog
    @RequestMapping(value="/timer/delete",method=RequestMethod.DELETE)
    public ResponseVo deleteTimer(@RequestBody GridTimerDelete delete, @RequestHeader(name=StarterConstant.TOKEN) String token){

        Assert.isTrue(delete.getExperimentId()!=null && delete.getStepTimerId()!=null && delete.getPlateId()!=null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        experimentPlateLogic.deleteTimer(delete.getExperimentId(),delete.getStepTimerId(),delete.getPlateId(),delete.getGridIds(),userInfo.getLabId(),userInfo.getLabMemberId());

        return ResponseVo.success();
    }

    /**
     * 多孔板删除试剂/样品
     * @param delete
     * @param token
     * @return
     */
    @AspectLog
    @RequestMapping(value="/reagent/delete",method=RequestMethod.DELETE)
    public ResponseVo deleteReagent(@RequestBody GridReagentDelete delete,@RequestHeader(name=StarterConstant.TOKEN) String token){

        Assert.isTrue(delete.getExperimentId()!=null && delete.getSourceId()!=null && !CollectionUtils.isEmpty(delete.getGridIds()) && delete.getType()!=null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        experimentPlateLogic.deleteReagent(delete.getExperimentId(),delete.getSourceId(),delete.getType(),delete.getGridIds(),userInfo.getLabId(),userInfo.getLabMemberId());

        return ResponseVo.success();
    }

    /**
     * 多孔板删除文本
     * @param delete
     * @param token
     * @return
     */
    @AspectLog
    @RequestMapping(value="/text/delete",method=RequestMethod.DELETE)
    public ResponseVo deleteText(@RequestBody GridTextDelete delete,@RequestHeader(name=StarterConstant.TOKEN) String token){

        Assert.isTrue(delete.getExperimentId()!=null && delete.getStepTextId()!=null && !CollectionUtils.isEmpty(delete.getGridIds()) && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        experimentPlateLogic.deleteText(delete.getExperimentId(),delete.getStepTextId(),delete.getGridIds(),userInfo.getLabId(),userInfo.getLabMemberId());

        return ResponseVo.success();
    }

    /**
     * 批量获取多孔板栅格详情 样品/试剂/时间/文本
     * @param token
     * @return
     */
    @RequestMapping(value="/grid/detail",method=RequestMethod.POST)
    public ResponseVo plateGridDetail(@RequestBody GridQueryVo params,@RequestHeader(name=StarterConstant.TOKEN) String token){

        Long experimentId = params.getExperimentId();
        Long plateId = params.getPlateId();
        List<Long> gridIds = params.getGridIds();

        Assert.isTrue(experimentId!=null && plateId!=null && !CollectionUtils.isEmpty(gridIds) && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        List<PlateGridDetailVo> details = experimentPlateLogic.getPlateGridDetail(experimentId,plateId,gridIds,userInfo.getLabId());

        return ResponseVo.success(details);
    }

    /**
     * 获取多孔板栅格详情 样品/试剂/时间/文本
     * @param experimentId
     * @param plateId
     * @param gridId
     * @param token
     * @return
     */
    @RequestMapping(value="/grid/detail",method=RequestMethod.GET)
    public ResponseVo plateGridDetail(Long experimentId,Long plateId,Long gridId,@RequestHeader(name=StarterConstant.TOKEN) String token){

        Assert.isTrue(experimentId!=null && plateId!=null && gridId!=null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        PlateGridDetailVo detail = experimentPlateLogic.getPlateGridDetail(experimentId,plateId,gridId,userInfo.getLabId());

        return ResponseVo.success(detail);
    }

    /**
     * 获取胶板时间列表
     * @param experimentId
     * @param plateId
     * @param token
     * @return
     */
    @RequestMapping(value="/timer/list", method=RequestMethod.GET)
    public ResponseVo plateTimerList(Long experimentId,Long plateId,@RequestHeader(name=StarterConstant.TOKEN) String token){

        Assert.isTrue(experimentId!=null && plateId!=null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        List<PlateTimerVo> timerVos = experimentPlateLogic.getPlateTimerList(experimentId,plateId,userInfo.getLabId());

        return ResponseVo.success(timerVos);
    }

	/**
	 * 根据实验ID获取多孔板列表
	 *
	 * @return
	 */
	@GetMapping("/getList")
	public ResponseVo getPlateList(@RequestParam Long experimentId,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		List<Plate> list = experimentPlateLogic.getPlateList(experimentId);
//		if(!CollectionUtils.isEmpty(list)) {
//			for (Iterator<Plate> iterator = list.iterator(); iterator.hasNext();) {
//				Plate plate = iterator.next();
//				if(plate.getType().equals(PlateTypeEnum.GUM_PLATE.getValue())) {
//					iterator.remove();
//				}
//			}
//		}
		return ResponseVo.success(list);
	}

    /**
     * 多孔板添加覆盖内容
     */
	@AspectLog
    @RequestMapping(value="/gumGridModify",method=RequestMethod.PUT)
    public ResponseVo gumPlateGridModify(@RequestBody GumGridModify modify,@RequestHeader(name = StarterConstant.TOKEN) String token){

        Assert.isTrue(modify!=null && modify.getExperimentId()!=null && modify.getPlateId()!=null
                && modify.getGridId()!=null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        experimentPlateLogic.gumPlateGridModify(modify,userInfo.getLabId(),userInfo.getLabMemberId());

        return ResponseVo.success();
    }

}
